package com.liuzhu.chaoshikucungunli.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.liuzhu.chaoshikucungunli.R;
import java.util.List;

public class BannerAdapter extends RecyclerView.Adapter<BannerAdapter.BannerViewHolder> {
    
    private List<BannerItem> bannerItems;
    
    public BannerAdapter(List<BannerItem> bannerItems) {
        this.bannerItems = bannerItems;
    }
    
    @NonNull
    @Override
    public BannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_banner, parent, false);
        return new BannerViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull BannerViewHolder holder, int position) {
        BannerItem item = bannerItems.get(position);
        if (item.isLayout()) {
            ((ViewGroup)holder.itemView).removeAllViews();
            View view = LayoutInflater.from(holder.itemView.getContext())
                    .inflate(item.getLayoutRes(), (ViewGroup) holder.itemView, true);
            view.setTag("stats_view");
        } else {
            holder.imageView.setImageResource(item.getImageRes());
            holder.textView.setText(item.getText());
        }
    }
    
    @Override
    public int getItemCount() {
        return bannerItems.size();
    }
    
    static class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        
        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.banner_image);
            textView = itemView.findViewById(R.id.banner_text);
        }
    }
    
    public static class BannerItem {
        private int imageRes;
        private int layoutRes;
        private String text;
        private boolean isLayout;
        
        public BannerItem(int imageRes, String text) {
            this.imageRes = imageRes;
            this.text = text;
            this.isLayout = false;
        }
        
        public BannerItem(int layoutRes, String text, boolean isLayout) {
            this.layoutRes = layoutRes;
            this.text = text;
            this.isLayout = true;
        }
        
        public boolean isLayout() {
            return isLayout;
        }
        
        public int getLayoutRes() {
            return layoutRes;
        }
        
        public int getImageRes() {
            return imageRes;
        }
        
        public String getText() {
            return text;
        }
    }
} 